package com.sky.controller.user;

import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Api(tags = "C端-订单接口")
@RestController
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO submitVO = orderService.submitOrder(ordersSubmitDTO);


        return Result.success(submitVO);
    }

    @GetMapping("/getEstimatedDeliveryTime")
    @ApiOperation("查询预计送达时间")
    public Result<String> getEstimatedDeliveryTime(){
        Date date = new Date(System.currentTimeMillis() + 1000 * 60 * 60);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate().format(formatter);

        return Result.success(formattedDate);
    }
}
